<?php
global $_GPC, $_W;

$GLOBALS['frames'] = $this->getMainMenu2();
function api_notice_increment($url,$data)
{
	$curl = curl_init();
	$a = strlen($data);
	$header = array("Content-Type: application/json; charset=utf-8","Content-Length: $a");
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl,CURLOPT_POST,1);
	curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;

}

if(checksubmit('submit')){

    $number = $_GPC['number'];
    if ($_GPC['num'] > $number) {
       message('生成数量不可以大于卡券剩余数量','','error');
    }else{

       message('<span style=" color:red;font-size:50px;">数据生成中！切勿关闭浏览器！</span><br>数据布局中.....',$this->createWebUrl('addcodedata',array('op'=>'indata','zs'=>$_GPC['num'],'cid'=>$_GPC['cid']),'success'));

    }
      
}
if ($_GPC['op'] == 'indata') {
   
    if ($_GPC['zs'] > 100) {
        $zs = $_GPC['zs'] / 100;
    } else {
        $zs = $_GPC['zs'];
    }
    $z = 10;
    if ($_GPC['d'] == '') {
        $d = 1;
    } else {
        $d = $_GPC['d'] + 1;
    }
    $x = 1;
    $x = 1;
    while ($x <= $zs) {

        $datav['cid'] = $_GPC['cid'];
        $datav['city_id'] = $_COOKIE['account_id'];
        $datav['uniacid'] = $_W['uniacid'];
       
        $datav['addtime'] = date("Y-m-d H:i:s");
        pdo_insert('zhtc_qrcode_data', $datav);
        $codeid = pdo_insertid();
        $code['code'] = md5($codeid);
        $account = pdo_update('zhtc_qrcode_data',$code,array('id'=>$codeid));


        $access_token = pdo_fetch("select * from ims_zhtc_access_token where id = 1");

        if ($access_token) {
        	
        	$time = $access_token['addtime'] + 6000;

        	if ($time <= time()) {
        		
        		$url_a  ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx55fededcb55944c2&secret=ed85afe26cd020cc7703836ecc0df0e2";
				$access_token = file_get_contents($url_a);
				$ac = json_decode($access_token,1);
				pdo_update('zhtc_access_token',array('access_token'=>$ac['access_token'],'addtime'=>time()),array('id'=>1));
        	}else{
        		$ac['access_token'] = $access_token['access_token'];
        	}

        }else{
        	$url_a  ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx55fededcb55944c2&secret=ed85afe26cd020cc7703836ecc0df0e2";
			$access_token = file_get_contents($url_a);
			$ac = json_decode($access_token,1);

			$acdata['access_token'] = $ac['access_token'];
			$acdata['addtime'] = time();
			pdo_insert('zhtc_access_token',$acdata);
        }



		
		$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ac['access_token'];
				
		$data = [
	        "page"=>"zh_tcwq/pages/lmredbag/lmredbag",
	        "scene"=>$code['code'],
	        ];
	    $post_data= json_encode($data,true);
		$qrcode = api_notice_increment($url,$post_data);

		$file_url = IA_ROOT."/addons/zh_tcwq/img/".$_GPC['cid']."/";
		if (!file_exists($file_url)) {
		    mkdir($file_url, 0777);
		}
		$jpg = $qrcode;
		$file = fopen($file_url.'/'.$codeid.'.png',"w");//打开文件准备写入
		fwrite($file,$jpg);//写入
		fclose($file);//关闭


       
        if ($account) {
            $intok++;
        } else {
            $intsb++;
        }
        $p = $intok;
        $x++;
    }
    $_GPC['x'] = $_GPC['x'] + $p;
    $_GPC['sb'] = $_GPC['sb'] + $intsb;
    if ($_GPC['x'] < $_GPC['zs']) {
       
        message('<span style=" color:red;font-size:50px;">数据生成中！切勿关闭浏览器！</span><br>数据布局成功！，当前第' . $d . '批 , 总生成数据' . $_GPC['x'] . '个, 失败数据 ' . $_GPC['sb'] . ' 个。', $this->createWebUrl('addcodedata', array('op'=>'indata',"zs" => $_GPC['zs'], "d" => $d, "z" => $z, "x" => $_GPC['x'], "cid" => $_GPC['cid'], "sb" => $_GPC['sb'])), 'success');
 
    } else {
        
        message('数据全部生成完毕,并且优化掉重复数据！', $this->createWebUrl('qrcodelist'), 'success');
    }
}

include $this->template('web/addcodedata');